﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        IRepositorioLiga repositorioLigas = new RepositorioLiga();

        public ActionResult Index()
        {
            ViewData["Message"] = "Bienvenidos el sistema de manejo de ligas!";

            List<Liga> ligas = repositorioLigas.EncontrarTodasLasLigas().ToList<Liga>();
            ViewData["Ligas"] = new SelectList(ligas);

            if (Request.Cookies["IdLiga"] != null)
            {
                HttpCookie infoLiga = Request.Cookies["IdLiga"];
                
                int idLiga = Convert.ToInt32(infoLiga.Value);
                    
                return RedirectToAction("Index", "Noticias", new { id = idLiga });
            }
            else
                Response.Cookies.Set(new HttpCookie("IdLiga", "1"));

            return View();
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            String liga = Request.Form["ligas"];
            Liga seleccion = repositorioLigas.getLiga(liga);

            if (Request.Cookies["IdLiga"] != null)
                Response.Cookies.Set(Request.Cookies["IdLiga"]);
            else
                Response.Cookies.Set(new HttpCookie("IdLiga", "1"));

            Response.Cookies["IdLiga"].Expires = DateTime.Now.AddYears(30);
            Response.Cookies["IdLiga"].Value = seleccion.IDLiga.ToString();

            return RedirectToAction("Details", "Ligas", new { id = seleccion.IDLiga });
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult CambiarLiga()
        {
            HttpCookie cookie = new HttpCookie("IdLiga");
            cookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(cookie);

            return RedirectToAction("Index");
        }
    }
}
